#include "musicslider.h"
#include "ui_musicslider.h"
#include <QMouseEvent>
#include <QDebug>

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);

    maxWidth=this->width(); //初始化maxwidth给成 当前窗口的宽度

    currentPos = 0; //这里也可以直接在ui界面中修改初始化
    moveSlider();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::setStep(float ration)
{
    currentPos = ration * maxWidth;
    moveSlider();
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSlider();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    // 注意：鼠标移动的时候，event->pos()必须在MusciSlider窗口内
    //        才能支持拖拽功能
    // 检测：event->pos()是否在MusicSlider的窗口矩形范围内
//    QRect musicSliderRect = QRect(0, 0, geometry().width(), geometry().height());
//    if(!musicSliderRect.contains(event->pos())) //musicSliderRect的窗口矩形内 是否包含 鼠标事件的pos(一个点)
//    {
//        qDebug()<<musicSliderRect<<"-"<<event->pos();
//        return;
//    }


    if(event->buttons() == Qt::LeftButton) //只有 左键被 持续按下的时候 才可以移动   buttons() 持续按下/多个按钮 button()仅按下
    {
        currentPos = event->pos().x();

        if(currentPos < 0)
        {
            currentPos = 0;
        }
        else if(currentPos > maxWidth)
        {
            currentPos = maxWidth;
        }
          moveSlider();
    }
}


void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSlider();

    emit setMusicSliderPositon((float)ui->outLine->width()/(float)maxWidth);// 发送信号给qqmusic 让其修改进度条
}

void MusicSlider::moveSlider()
{
    ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(),currentPos,ui->outLine->height());
}
